package vip.zhenzicheng.nettybasic.checkread;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.logging.Logger;

/**
 * @author zhenzicheng
 * @date 2022-06-12 20:18
 */
public class EchoServerCRHandler extends ChannelInboundHandlerAdapter {

  private static final Logger LOG = Logger.getLogger(EchoServerCRHandler.class.getSimpleName());
  private int readCount = 0;
  private int readCompleteCount = 0;

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    //ByteBuf in = (ByteBuf)msg;
    LOG.info("channelRead执行了" + (++readCount) + "次");
    //ctx.writeAndFlush(in);
    ctx.fireChannelRead(msg);
  }

  @Override
  public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    LOG.info("channelReadComplete执行了" + (++readCompleteCount) + "次");
    super.channelReadComplete(ctx);
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    cause.printStackTrace();
    ctx.close();
  }
}
